package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetIhomeBean;
import cn.bocweb.spinning.util.DeviceUtil;

/**
 * Created by chenchangxing on 16/7/11.
 */
public class InfoSearchAdapter extends RecyclerView.Adapter {

    List<GetIhomeBean.DataEntity> mList;
    OnItemClickListener mOnItemClickListener;
    Context mContext;

    public InfoSearchAdapter(List<GetIhomeBean.DataEntity> list) {
        this.mList = list;
    }

    @Override
    public int getItemViewType(int position) {
        switch (Integer.parseInt(mList.get(position).getYs())) {
            case 1:
                return 0;
            case 2:
                return 1;
            case 3:
                return 2;
            case 4:
                return 3;
            case 5:
                return 4;
            default:
                return super.getItemViewType(position);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        switch (viewType) {
            case 0:
                View view0 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_profession_info_img_text, parent, false);
                return new ViewHolder0(view0);
            case 1:
                View view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_profession_info_text, parent, false);
                return new ViewHolder1(view1);
            case 2:
                View view2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_profession_info_img, parent, false);
                return new ViewHolder2(view2);
            case 3:
                View view3 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_profession_info_vedio_text, parent, false);
                return new ViewHolder3(view3);
            case 4:
                View view4 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_profession_info_video, parent, false);
                return new ViewHolder4(view4);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                ViewHolder0 holder0 = (ViewHolder0) holder;
                holder0.professionInfoImgTextTitle.setText(mList.get(position).getTitle());
                holder0.professionInfoImgTextContent.setText(mList.get(position).getContent());
                holder0.professionInfoImgTextTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(mList.get(position).getTimeline()))));
                String img = mList.get(position).getImg().get(0);
                if (!(img == null || img.isEmpty())) {
                    Glide.with(mContext).load(mList.get(position).getImg().get(0)).into(holder0.professionInfoImgTextImg);
                }
                break;
            case 1:
                ViewHolder1 holder1 = (ViewHolder1) holder;
                holder1.professionInfoTextTitle.setText(mList.get(position).getTitle());
                holder1.professionInfoTextContent.setText(mList.get(position).getContent());
                holder1.professionInfoTextTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(mList.get(position).getTimeline()))));
                break;
            case 2:
                ViewHolder2 holder2 = (ViewHolder2) holder;
                holder2.professionInfoImgTitle.setText(mList.get(position).getTitle());
                holder2.professionInfoImgTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(mList.get(position).getTimeline()))));

                /**
                 * 可能多图的资讯没有固定的三张图片.
                 */
                String img1Path = mList.get(position).getImg().size() >= 1 ? mList.get(position).getImg().get(0): null;
                String img2Path = mList.get(position).getImg().size() >= 2 ? mList.get(position).getImg().get(1): null;
                String img3Path = mList.get(position).getImg().size() >= 3 ? mList.get(position).getImg().get(2): null;

                if (!(img1Path == null || img1Path.isEmpty())) {
                    Glide.with(mContext).load(mList.get(position).getImg().get(0)).into(holder2.professionInfoImgImg1);
                }
                if (!(img2Path == null) || img2Path.isEmpty()) {
                    Glide.with(mContext).load(mList.get(position).getImg().get(1)).into(holder2.professionInfoImgImg2);
                }
                if (!(img3Path == null) || img3Path.isEmpty()) {
                    Glide.with(mContext).load(mList.get(position).getImg().get(2)).into(holder2.professionInfoImgImg3);
                }
                break;
            case 3:
                ViewHolder3 holder3 = (ViewHolder3) holder;
                holder3.professionInfoVedioTextTitle.setText(mList.get(position).getTitle());
                holder3.professionInfoVedioTextContent.setText(mList.get(position).getContent());
                holder3.professionInfoVedioTextTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(mList.get(position).getTimeline()))));
                String img3 = mList.get(position).getImg().get(0);
                if (!(img3 == null || img3.isEmpty())) {
                    Glide.with(mContext).load(mList.get(position).getImg().get(0)).into(holder3.professionInfoVedioTextImg);
                }
                break;
            case 4:
                ViewHolder4 holder4 = (ViewHolder4) holder;
                holder4.professionInfoVideoTitle.setText(mList.get(position).getTitle());
                holder4.professionInfoVideoTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(mList.get(position).getTimeline()))));
                String img4 = mList.get(position).getImg().get(0);
                if (!(img4 == null || img4.isEmpty())) {
                    Glide.with(mContext).load(mList.get(position).getImg().get(0)).into(holder4.professionInfoVideoImg);
                }
                break;
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.click(mList.get(holder.getAdapterPosition()).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder0 extends RecyclerView.ViewHolder {
        @Bind(R.id.profession_info_img_text_img)
        ImageView professionInfoImgTextImg;
        @Bind(R.id.profession_info_img_text_title)
        TextView professionInfoImgTextTitle;
        @Bind(R.id.profession_info_img_text_content)
        TextView professionInfoImgTextContent;
        @Bind(R.id.profession_info_img_text_time)
        TextView professionInfoImgTextTime;

        ViewHolder0(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.profession_info_text_title)
        TextView professionInfoTextTitle;
        @Bind(R.id.profession_info_text_content)
        TextView professionInfoTextContent;
        @Bind(R.id.profession_info_text_time)
        TextView professionInfoTextTime;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 extends RecyclerView.ViewHolder {
        @Bind(R.id.profession_info_img_title)
        TextView professionInfoImgTitle;
        @Bind(R.id.profession_info_img_time)
        TextView professionInfoImgTime;
        @Bind(R.id.profession_info_img_img1)
        ImageView professionInfoImgImg1;
        @Bind(R.id.profession_info_img_img2)
        ImageView professionInfoImgImg2;
        @Bind(R.id.profession_info_img_img3)
        ImageView professionInfoImgImg3;

        ViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder3 extends RecyclerView.ViewHolder {
        @Bind(R.id.profession_info_vedio_text_img)
        ImageView professionInfoVedioTextImg;
        @Bind(R.id.profession_info_vedio_text_title)
        TextView professionInfoVedioTextTitle;
        @Bind(R.id.profession_info_vedio_text_content)
        TextView professionInfoVedioTextContent;
        @Bind(R.id.profession_info_vedio_text_time)
        TextView professionInfoVedioTextTime;

        ViewHolder3(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder4 extends RecyclerView.ViewHolder {
        @Bind(R.id.profession_info_video_title)
        TextView professionInfoVideoTitle;
        @Bind(R.id.profession_info_video_time)
        TextView professionInfoVideoTime;
        @Bind(R.id.profession_info_video_img)
        ImageView professionInfoVideoImg;

        ViewHolder4(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void click(String infoId);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
